package chixing.day20.work4;

import java.util.Random;

public class Test4 {
    public static void main(String[] args) {
        DispatchService dispatchService = new DispatchService();
        Random random = new Random();

        // 初始化 5 名骑手，坐标随机，maxCapacity=2
        String[] riderNames = {"张三", "李四", "王五", "赵六", "钱七"};
        for (int i = 0; i < 5; i++) {
            Location riderLocation = new Location(random.nextInt(10), random.nextInt(10));
            Rider rider = new Rider(i + 1, riderNames[i], true, riderLocation, 3);
            dispatchService.addRider(rider);
        }

        // 初始化 3 个商家
        Location store1Location = new Location(2, 2);
        Location store2Location = new Location(5, 5);
        Location store3Location = new Location(8, 8);

        // 初始化 10 个订单，设置不同优先级、不同距离
        OrderPriority[] priorities = {OrderPriority.HIGH, OrderPriority.MEDIUM, OrderPriority.LOW};
        for (int i = 0; i < 10; i++) {
            Location customerLocation = new Location(random.nextInt(10), random.nextInt(10));
            double distance = customerLocation.distanceTo(store1Location);
            OrderPriority priority = priorities[random.nextInt(priorities.length)];
            int expectedDeliveryTime = 15 + random.nextInt(30); // 15-45分钟
 
            Order order = new Order(i + 1, customerLocation, distance, priority, expectedDeliveryTime);
            dispatchService.addOrder(order);
        }

        System.out.println("\n=== 开始批量派单 ===");
        dispatchService.batchDispatch();

        System.out.println("\n=== 派单后统计 ===");
        dispatchService.printRiderStatistics();
        // 模拟部分骑手完成订单
        System.out.println("\n=== 模拟订单完成 ===");
        for (int i = 0; i < 3; i++) {
            // 随机选择一个有订单的骑手
            dispatchService.getAvailableRiders().stream()
                    .filter(rider -> !dispatchService.getOrdersByRider(rider).isEmpty())
                    .findFirst()
                    .ifPresent(rider -> {
                        // 随机选择该骑手的一个订单完成
                        java.util.List<Order> orders = dispatchService.getOrdersByRider(rider);
                        if (!orders.isEmpty()) {
                            Order order = orders.get(0);
                            dispatchService.finishDelivery(rider, order);
                        }
                    });
        }

    }
}
